/*
 * @Author: mulingyuer
 * @Date: 2023-03-24 13:26:05
 * @LastEditTime: 2023-03-24 13:26:05
 * @LastEditors: mulingyuer
 * @Description: 评论列表
 * @FilePath: \Typecho_Theme_JJ\src\modules\comment\comment_list\index.ts
 * 怎么可能会有bug！！！
 */
import "./style.scss";
import CommentTextarea from "@/modules/comment/comment_form";
class Comment {
	private formWrap: HTMLElement;
	private commentList: HTMLElement;
	private pidInput: HTMLInputElement;
	private commentformcancel: HTMLElement;

	constructor() {
		this.formWrap = document.querySelector(".comment-form-wrap") as HTMLElement;
		this.commentList = document.querySelector(".comment-list-wrapper") as HTMLElement;
		this.pidInput = document.querySelector("input[name=pid]") as HTMLInputElement;
		this.commentformcancel = document.querySelector(".comment-form-wrap .comment-form-cancel") as HTMLElement;
	}

	reply_link(cid: number) {
		this.pidInput = document.querySelector("input[name=pid]") as HTMLInputElement;
		this.pidInput.value = cid + "";
		let formHtml = document.createElement("div");
		formHtml.innerHTML = this.formWrap.innerHTML;
		formHtml.classList.add("comment-form-wrap");
		let comment = document.querySelector(`#comment-${cid}`);
		comment?.insertAdjacentElement("afterend", formHtml);
		this.formWrap.remove();
		this.formWrap = formHtml;
		this.commentformcancel = document.querySelector(".comment-form-wrap .comment-form-cancel") as HTMLElement;
		this.commentformcancel.classList.remove("hidden");
		new CommentTextarea();
		this.cancel_comment(cid);
	}

	cancel_comment(cid: number) {
		this.commentformcancel.addEventListener("click", (e: Event) => {
			this.pidInput = document.querySelector("input[name=pid]") as HTMLInputElement;
			this.pidInput.value = "";
			this.commentList.insertAdjacentElement("beforebegin", this.formWrap);
			this.commentformcancel.classList.add("hidden");
		});
	}
}

let comment = new Comment();

window.comment = comment;
